package com.qingcheng.controller;

import com.qingcheng.entity.PageResult;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.goods.Sku;
import com.qingcheng.handler.InsElasticSearchHandler;
import com.qingcheng.pojo.goods.SkuPandect;
import com.qingcheng.pojo.goods.Spu;
import com.qingcheng.pojo.goods.TbSku;
import com.qingcheng.pojo.order.OrderItem;
import com.qingcheng.service.SkuSearchService;
import com.qingcheng.service.SkuService;
import com.qingcheng.service.TbSkuService;
import com.qingcheng.util.PageUtil;
import com.qingcheng.util.YouthR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sku")
@Api(tags = "TbSkuController")
public class SkuController {

    @Autowired
    private SkuService skuService;

    @Resource
    private TbSkuService tbSkuService;

    @Autowired
    private SkuSearchService skuSearchService;

    @Autowired(required = false)
    private InsElasticSearchHandler es;

    @ApiOperation(value = "简单的es查询")
    @GetMapping("search")
    public Map searchSkuES(@RequestParam Map<String, String> map) {
        Map search = new HashMap();
        try {
            search = skuSearchService.search(map);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return search;
    }


    @ApiOperation(value = "批量插入数据到ElasticSearch")
    @GetMapping("initES/{tbSkus}/{args}")
    public YouthR initES(@PathVariable("tbSkus") List<TbSku> tbSkus,@PathVariable("args") String args) {
        try {
            es.insOrDel(tbSkus,args);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return YouthR.ok();
    }

    @ApiOperation(value = "根据id获取信息")
    @GetMapping("/detail/{id}")
    public YouthR detail(@PathVariable Integer id) {

        TbSku tbSku = tbSkuService.findById(id);
        return YouthR.ok().data("tbSku",tbSku);
    }

    @ApiOperation(value = "获取信息（list不分页）")
    @GetMapping("/list")
    public YouthR list() {
        List<TbSku>  list = tbSkuService.queryAll();
        return YouthR.ok().data("list",list);
    }



    @ApiOperation(value = "获取信息（list分页）不带条件")
    @GetMapping("/listPages")
    public YouthR listPages(Integer pageNum,Integer pageSize) {
        PageUtil<TbSku> pageUtils = tbSkuService.findListAll(pageNum, pageSize);

        return YouthR.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "获取信息（list分页）带条件")
    @PostMapping("pageQueryByAll/{pageNum}/{pageSize}")
    public YouthR pageQueryByAll(@PathVariable Integer pageNum,
                                 @PathVariable Integer pageSize,
                                 @RequestBody TbSku tbSku){
        PageUtil<TbSku> pageUtils = tbSkuService.pageQueryByAll(pageNum, pageSize, tbSku);
        return YouthR.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "添加信息")
    @PostMapping("/addSku")
    public YouthR addSku(@RequestBody TbSku tbSku) {

        boolean b = tbSkuService.saveTbSku(tbSku);
        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @ApiOperation(value = "修改信息")
    @PutMapping("/update")
    public YouthR update(@RequestBody TbSku tbSku) {
        boolean b = tbSkuService.updateTbSku(tbSku);

        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @ApiOperation(value = "删除信息（主键）")
    @DeleteMapping("/delete/{id}")
    public YouthR delete(@PathVariable Integer id) {
        boolean b = tbSkuService.deleteById(id);
        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @GetMapping("/findAll")
    public List<Sku> findAll(){
        return skuService.findAll();
    }

    @GetMapping("/findPage")
    public PageResult<Sku> findPage(int page, int size){
        return skuService.findPage(page, size);
    }

    @PostMapping("/findList")
    public List<Sku> findList(@RequestBody Map<String,Object> searchMap){
        return skuService.findList(searchMap);
    }

    @PostMapping("/findPage")
    public PageResult<Sku> findPage(@RequestBody Map<String,Object> searchMap,int page, int size){
        return  skuService.findPage(searchMap,page,size);
    }

    @GetMapping("/findById")
    public Sku findById(String id){
        return skuService.findById(id);
    }

    /**
     * 商品新增广播通知
     * @param sku
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Sku sku){
        sku.setStatus("1");
        skuService.add(sku);
        return new Result(200,"添加成功",null);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Sku sku){
        skuService.update(sku);
        return new Result(200,"修改成功",null);
    }

    /**
     * 广播删除数据库数据连同elasticsearch删除
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(String id){
        skuService.delete(id);
        return new Result(200,"删除成功",null);
    }

    /**
     * 查询商品数量
     * @return
     */
    @GetMapping("/findSum")
    public Result findSum(){
        SkuPandect sum = skuService.findSum();
        return new Result(200,"查询成功",sum);
    }

    /**
     * 商品下架广播通知
     * @param id
     */
    @PostMapping("/isDelete")
    public  Result isDelete(String id){
        this.skuService.isDelete(id);
        return new Result(200,"下架成功",null);
    }

    @PostMapping("/deductionStock")
    public boolean deductionStock(@RequestBody List<OrderItem> orderItemList){
        return skuService.deductionStock(orderItemList);
    }

    /**
     * 商品广播上架
     */
    @PostMapping("isUpdate")
     public  Result isUpdate(String id){
        this.skuService.isUpdate(id);
        return new Result(200,"上架成功",null);
    }

    @PostMapping("/searchGoodsByGoodname")
    public Result searchGoodsByGoodname(String name){
        List<Sku> skus =  skuService.searchGoodsByGoodname(name);
        return new Result(200,"查询成功成功",skus);
    }
}
